from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from goods.models import Brand, SPUSpecification, SPU, SpecificationOption, GoodsCategory
from meiduo_admin.serializers.brands import BrandSerializer, SpecsSerializer, SpecsSimpleSerializer, OptionSerializer, \
    OptionSimpleSerializer, SPUSerializer, BrandSimpleSerializer, CategorySerializer


class BrandViewSet(ModelViewSet):
    permission_classes = [IsAdminUser]

    lookup_value_regex = '\d+'

    queryset = Brand.objects.all()
    serializer_class = BrandSerializer


class SpecsViewSet(ModelViewSet):
    permission_classes = [IsAdminUser]

    lookup_value_regex = '\d+'

    queryset = SPUSpecification.objects.all()

    serializer_class = SpecsSerializer

    def simple(self, request):
        queryset = SPU.objects.all()
        serializer = SpecsSimpleSerializer(queryset, many=True)

        return Response(serializer.data)


class OptionViewSet(ModelViewSet):
    permission_classes = [IsAdminUser]

    lookup_value_regex = '\d+'

    queryset = SpecificationOption.objects.all()

    serializer_class = OptionSerializer

    def simple(self, request):
        specs = SPUSpecification.objects.all()
        serializer = OptionSimpleSerializer(specs, many=True)

        return Response(serializer.data)


class SPUViewSet(ModelViewSet):
    permission_classes = [IsAdminUser]
    lookup_value_regex = '\d+'

    queryset = SPU.objects.all()

    serializer_class = SPUSerializer

    def simple(self, request):
        brand = Brand.objects.all()
        serializer = BrandSimpleSerializer(brand, many=True)

        return Response(serializer.data)

    def category_1(self, request):
        category_1 = GoodsCategory.objects.filter(parent__isnull=True)

        serializer = CategorySerializer(category_1, many=True)

        return Response(serializer.data)

    def category_2_3(self, request, pk):
        category_2_3 = GoodsCategory.objects.filter(parent_id=pk)

        serializer = CategorySerializer(category_2_3, many=True)

        return Response(serializer.data)



